import prompt from '@system.prompt'
import router from '@system.router';

const ACTION_SYNC = 0;
const ACTION_ASYNC = 1;

export default {
    data: {
        title: "",
        image_array: [{
                          src: "/common/images/bg-tv.jpg",
                          check: false
                      }],
        imageCount: 1, //最大选择数量
        selectImage: [], //传递过来已经选择的
    },
    onInit() {
        this.title = this.$t('strings.world');
    },
    onReady() {
        this.getImageData();
    },
    itemClick: function (index) { //list-item点击事件
        if (this.getCheckNum() == this.imageCount
        && !this.image_array[index].check) {
            this.showToast("最多只能选择" + this.imageCount + "张图片")
            return
        }
        this.image_array[index].check = !this.image_array[index].check;
    },
    selectOk: function () {
        if (this.getCheckNum() == 0) {
            this.showToast("请选择至少1张图片");
            return;
        }
        var selectImage = [];
        this.image_array.forEach(function (item) {
            if (item.check) {
                selectImage.push(item.src);
            }
        })
        //临时缓存到app.js
        var appData = this.$app.$def.data;
        appData.imageSelect = selectImage;
        //返回上级页面
        router.back();
    },
    initAction: function (code, sync) {
        var actionData = {};
        var action = {};
        action.bundleName = "com.monkey.imageselect";
        action.abilityName = "ImageDataInternalAbility";
        action.messageCode = code;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = sync;
        return action;
    },
    getImageData: async function () { //从javaPA 加载图片数据
        try {
            var action = this.initAction(1001, ACTION_ASYNC);
            var result = await FeatureAbility.callAbility(action);
            var json = JSON.parse(result);
            if (json.code == 0) {
                console.log("callAbility json " + json.data);
                if (json.data.length > 0) {
                    this.image_array.splice(0, this.image_array.length);
                    for (var i = 0; i < json.data.length; i++) {
                        //遍历数据转换格式
                        var checked = this.isCheck(json.data[i]);
                        this.image_array.push({
                            src: json.data[i],
                            check: checked
                        })
                    }
                } else {
                    //没有数据
                }
            } else {
                //出现错误
                this.showToast("加载图片出错，请检查权限！");
            }
        } catch (pluginError) {
            console.error("getImageData : Plugin Error = " + pluginError);
        }
    },
    showToast: function (msg) {
        prompt.showToast({
            message: msg
        });
    },
    isCheck: function (path) { //检查图片路径是否被选中
        for (var i = 0; i < this.selectImage.length; i++) {
            if (this.selectImage[i] === path) {
                return true;
            }
        }
        return false;
    },
    getCheckNum: function () { //获取选中的图片数量
        var num = 0;
        this.image_array.forEach(function (item) {
            if (item.check) {
                num++;
            }
        })
        return num;
    },
}
